package com.wingscode.modules.app.utils;

import com.alibaba.fastjson.JSONObject;
import com.wingscode.common.utils.HttpUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static com.wingscode.MyConfig.MINI_APPID;
import static com.wingscode.MyConfig.MINI_APPSECRET;

/**
 * @author CrazyHuskar
 * caeat at 2019/7/30  15:48
 */
public class WXUtil {
    public JSONObject getOpenId(String code) throws IOException {
        String requestUrl = "https://api.weixin.qq.com/sns/jscode2session";
        Map<String, String> requestUrlParam = new HashMap<String, String>();
        requestUrlParam.put("appid", MINI_APPID);	//小程序 appId
        requestUrlParam.put("secret", MINI_APPSECRET);	//小程序 appSecret
        requestUrlParam.put("js_code", code);	//登录时获取的 code
        requestUrlParam.put("grant_type", "authorization_code");	//授权类型，此处固定
        JSONObject jsonObject = JSONObject.parseObject(HttpUtils.doGet(requestUrl, requestUrlParam));

        return jsonObject;
    }
}
